package org.example.config;

import cn.hutool.core.util.ObjectUtil;
import org.example.constant.AuthConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;

@Component
public class FeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //获取请求对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (ObjectUtil.isNotEmpty(requestAttributes)){
            HttpServletRequest request = requestAttributes.getRequest();
            if (ObjectUtil.isNotEmpty(request)){
                //获取token值，传递到下一个请求对象的请求头
                String authorization = request.getHeader(AuthConstants.AUTHORIZATION);
                requestTemplate.header(AuthConstants.AUTHORIZATION,authorization);
                return;
            }
        }
        requestTemplate.header(AuthConstants.AUTHORIZATION,AuthConstants.BEARER + "0ab7a71a-5a84-4c38-8040-9056cf2ca94b");
    }
}
